package com.example.intermediate_control;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;



public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private DatePicker datePicker;
    private TextView tvPickerDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);
        datePicker = findViewById(R.id.dp_date);
        tvPickerDate = findViewById(R.id.tv_pickerDate);
        findViewById(R.id.btn_date).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                String desc = String.format("您选择打日期是%d年%d月%d日", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth());
                tvPickerDate.setText(desc);
                break;
            case R.id.btn_date:
                //获取日历的一个实例，里面包含了当前的年月日
//                Calendar calendar = Calendar.getInstance();
//                calendar.get(Calendar.YEAR);
                DatePickerDialog dialog = new DatePickerDialog(this,this,2022,7,14);
                //显示日期对话框
                dialog.show();
                break;
        }
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
        String desc = String.format("您选择打日期是%d年%d月%d日", year, month + 1, dayOfMonth);
        tvPickerDate.setText(desc);
    }
}